package flow.action;

import flow.model.*;
import flow.service.FlowBusinessService;
import flow.service.FlowDefineService;
import flow.service.FlowInstanceService;
import flow.util.FlowUtil;
import m.common.action.ActionMeta;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import manage.action.ManageAction;
import manage.model.AdminLogin;

import java.util.ArrayList;
import java.util.List;

@ActionMeta(name="flow/flow")
public class FlowAction extends ManageAction {
	private FlowBusiness business;
	private FlowDefine define;
	private FlowInstance instance;
	private FlowInstanceSection instanceSection;
	private List<FlowSectionLink> sectionLinks;
	private FlowSectionLink selectLink;
	private String opinion;
	private List<AdminLogin> adminList= new ArrayList<>();

	public JSONMessage doStart() {
		JSONMessage result=new JSONMessage();
		try {
			AdminLogin admin=getSessionAdmin();
			if(null==admin) throw noLoginException;
			business=getService(FlowBusinessService.class).toFlowBusiness(business.getOid());
			fillJSONResult(result,true,getService(FlowInstanceService.class).doStart(business));
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public JSONMessage doProcess() {
		JSONMessage result=new JSONMessage();
		try {
			AdminLogin admin=getSessionAdmin();
			if(null==admin) throw noLoginException;
			fillJSONResult(result,true,getService(FlowInstanceService.class).doProcess(admin,instance, selectLink, adminList,opinion));
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}

	public JSONMessage getFlowInfo() {
		JSONMessage result=new JSONMessage();
		try {
			AdminLogin admin=getSessionAdmin();
			if(null==admin) throw noLoginException;
			if(null==business) throw new MException(this.getClass(), "参数错误");
			business=getService(FlowBusinessService.class).toFlowBusiness(business.getOid());
			instance=FlowUtil.getInstance4Business(business.getBusiService(), business.getOid());
			List<FlowInstanceSection> instanceSectionList;
			if(null!=instance) {
				define=instance.getFlowDefine();
				if("N".equals(instance.getDoneStatus())) {
					instanceSection=getService(FlowInstanceService.class).getInstanceSection(instance.getOid(), instance.getCurrentSection().getOid(), admin.getOid(), null);
					sectionLinks=ModelQueryList.getModelList(
						FlowSectionLink.class, new String[] {"*","toSection.*"}, null, 
						QueryCondition.and(
							QueryCondition.eq("fromSection.oid", instance.getCurrentSection().getOid()),
							QueryCondition.in("fromSection.oid", 
								ModelQueryList.instance(FlowInstanceSection.class, new String[] {"flowSection.oid"}, null, 
									QueryCondition.and(
										QueryCondition.eq("flowInstance.oid", instance.getOid()),
										QueryCondition.eq("user.oid", admin.getOid()),
										QueryCondition.eq("doneStatus", "N")
									)
								)
							)
						),
						QueryOrder.asc("isNext"),QueryOrder.asc("toSection.identity")
					);
				}
				instanceSectionList=ModelQueryList.getModelList(FlowInstanceSection.class, new String[] {"*","flowSection.*","user.*"}, null, 
					QueryCondition.eq("flowInstance.oid", instance.getOid()),
					QueryOrder.asc("acceptDate")
				);
			}else {
				define=getService(FlowDefineService.class).getIssue(FlowUtil.getFlowId(business.getBusiService()));
				instance=new FlowInstance();
				instanceSectionList= new ArrayList<>();
			}
			result.push("business",business);
			result.push("instance",instance);
			result.push("define",define);
			result.push("instanceSection",instanceSection);
			result.push("sectionLinks",sectionLinks);
			result.push("instanceSectionList",instanceSectionList);
			fillJSONResult(result, true, "");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public FlowBusiness getBusiness() {
		return business;
	}

	public void setBusiness(FlowBusiness business) {
		this.business = business;
	}

	public FlowDefine getDefine() {
		return define;
	}

	public void setDefine(FlowDefine define) {
		this.define = define;
	}

	public FlowInstance getInstance() {
		return instance;
	}

	public void setInstance(FlowInstance instance) {
		this.instance = instance;
	}

	public FlowInstanceSection getInstanceSection() {
		return instanceSection;
	}

	public void setInstanceSection(FlowInstanceSection instanceSection) {
		this.instanceSection = instanceSection;
	}

	public List<FlowSectionLink> getSectionLinks() {
		return sectionLinks;
	}

	public void setSectionLinks(List<FlowSectionLink> sectionLinks) {
		this.sectionLinks = sectionLinks;
	}

	public FlowSectionLink getSelectLink() {
		return selectLink;
	}

	public void setSelectLink(FlowSectionLink selectLink) {
		this.selectLink = selectLink;
	}

	public List<AdminLogin> getAdminList() {
		return adminList;
	}

	public void setAdminList(List<AdminLogin> adminList) {
		this.adminList = adminList;
	}
	public String getOpinion() {
		return opinion;
	}
	public void setOpinion(String opinion) {
		this.opinion = opinion;
	}

}
